package cn.iocoder.yudao.module.book.controller.admin.collections;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.collections.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.collections.CollectionsDO;
import cn.iocoder.yudao.module.book.service.collections.CollectionsService;

@Tag(name = "管理后台 - 绘本集")
@RestController
@RequestMapping("/book/collections")
@Validated
public class CollectionsController {

    @Resource
    private CollectionsService collectionsService;

    @PostMapping("/create")
    @Operation(summary = "创建绘本集")
    @PreAuthorize("@ss.hasPermission('book:collections:create')")
    public CommonResult<Integer> createCollections(@Valid @RequestBody CollectionsSaveReqVO createReqVO) {
        return success(collectionsService.createCollections(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新绘本集")
    @PreAuthorize("@ss.hasPermission('book:collections:update')")
    public CommonResult<Boolean> updateCollections(@Valid @RequestBody CollectionsSaveReqVO updateReqVO) {
        collectionsService.updateCollections(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除绘本集")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:collections:delete')")
    public CommonResult<Boolean> deleteCollections(@RequestParam("id") Integer id) {
        collectionsService.deleteCollections(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得绘本集")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:collections:query')")
    public CommonResult<CollectionsRespVO> getCollections(@RequestParam("id") Integer id) {
        CollectionsDO collections = collectionsService.getCollections(id);
        return success(BeanUtils.toBean(collections, CollectionsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得绘本集分页")
    @PreAuthorize("@ss.hasPermission('book:collections:query')")
    public CommonResult<PageResult<CollectionsRespVO>> getCollectionsPage(@Valid CollectionsPageReqVO pageReqVO) {
        PageResult<CollectionsDO> pageResult = collectionsService.getCollectionsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CollectionsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出绘本集 Excel")
    @PreAuthorize("@ss.hasPermission('book:collections:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCollectionsExcel(@Valid CollectionsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CollectionsDO> list = collectionsService.getCollectionsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "绘本集.xls", "数据", CollectionsRespVO.class,
                        BeanUtils.toBean(list, CollectionsRespVO.class));
    }

}